package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.workcellinfo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.EntityAbnormalTypeMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.AbnormalType;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.workcellinfo.JsmAbnormalTypeService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by huangxueqian@cqhilink.com on 2016/12/12.
 * 异常类型服务实现类
 */

public class JsmAbnormalTypeServiceImpl implements JsmAbnormalTypeService {

    @Autowired
    public EntityAbnormalTypeMapper entityAbnormalTypeMapper;

    /**
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject table(AbnormalType abnormalType) throws Exception {
        JSONObject jsonObject = new JSONObject();
        /* 进行分页查询分离 */
        jsonObject.put("total", entityAbnormalTypeMapper.queryCount(abnormalType));
        List<AbnormalType> abnormalTypeList = entityAbnormalTypeMapper.queryTableList(abnormalType);
        jsonObject.put("rows", JSON.toJSON(abnormalTypeList));
        return jsonObject;
    }

    /**
     * @param abnormalType 新增异常类型
     * @return
     * @throws Exception
     */
    @Override
    public Integer addAbnormalType(AbnormalType abnormalType, Employee employee) throws Exception {
        Integer resInt = 0;
        abnormalType.setModifierId(employee.getEmployeeId());
        abnormalType.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
        if (abnormalType.getAbnormalTypeId() == null) {
            abnormalType.setAbnormalTypeId(UniqueUtil.uuid());
            abnormalType.setCreatorId(employee.getEmployeeId());
            abnormalType.setCreateEtime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            abnormalType.setStatus(Short.valueOf("0"));
            resInt = entityAbnormalTypeMapper.insertSelective(abnormalType);
        } else {
            resInt = entityAbnormalTypeMapper.updateByPrimaryKeySelective(abnormalType);
        }
        return resInt;
    }

    /**
     * 删除异常类型
     *
     * @param abnormalTypeIds
     * @return
     */
    @Override
    public Integer delAbnormalType(Map abnormalTypeIds) {

        return entityAbnormalTypeMapper.deleteByPrimaryAbnormalIds(abnormalTypeIds);
    }

    /**
     * 查询异常类型
     * @param abnormalTypeId
     * @return
     */
    @Override
    public AbnormalType queryAbnormalType(String abnormalTypeId) throws Exception {
        return entityAbnormalTypeMapper.selectByPrimaryKey(abnormalTypeId);
    }
}
